﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Viewer.BusinessObject.BusinessEntities;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCThayDoiCoQuanSuDung : System.Web.UI.UserControl
    {
        CommonCode common = new CommonCode();
        NSNhanVien nhanVien = new NSNhanVien();
        NSToChuc toChuc = new NSToChuc();
        NSQuaTrinhCongTac qtct = new NSQuaTrinhCongTac();
        DataClassesDataContext db = new DataClassesDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            dtNgayThayDoi.Text = DateTime.Now.ToString("dd/MM/yyyy");

        }
        protected void btnSaveThayDoiCoQuan_CLick(object sender, DirectEventArgs e)
        {
            int quyetDinhID = UCQuyetDinh.TaoMoiQuyetDinh();

            int idToChucThayDoi = !string.IsNullOrEmpty(hdToChucThayDoiId.Value.ToString()) ? Convert.ToInt32(hdToChucThayDoiId.Value) : 0;
            if (!txtCoQuanSuDung_ThayDoiCoQuan.Text.Equals(lblCoQuanSuDungHienTai))
            {
                DateTime ngayBatDau = !dtNgayThayDoi.Text.Equals(0001) ? Convert.ToDateTime(dtNgayThayDoi.Text) : DateTime.MinValue;
                int nhanVienid = Convert.ToInt32(hdNhanVienId.Text);

                //Update table Nhân viên
                NSNhanVien item = (from a in db.NSNhanViens
                                   where a.NhanVienID.Equals(nhanVienid)
                                   select a).FirstOrDefault();
                item.CoQuanQuanLiId = idToChucThayDoi;


                //Update table Quá trinh công tác
                NSQuaTrinhCongTac itemQTCT = (from a in db.NSQuaTrinhCongTacs
                                              where a.NhanVienId.Equals(nhanVienid)
                                              orderby a.NgayBatDau descending
                                              select a).FirstOrDefault();

                if (itemQTCT != null)
                {
                    itemQTCT.NgayKetThuc = ngayBatDau;
                    //itemQTCT.CoQuanSuDungID = Convert.ToInt32(hdToChucThayDoiId.Text);
                }


                NSQuaTrinhCongTac newItem = new NSQuaTrinhCongTac();
                newItem.NgayBatDau = ngayBatDau;
                newItem.CoQuanSuDungID = Convert.ToInt32(hdToChucThayDoiId.Text);
                newItem.ToChucFlag = true;
                newItem.NoiDung = txtGhiChu.Text;
                newItem.ChucVu = txtChucVu.Text;
                newItem.CongViec = txtCongViec.Text;
                newItem.NhanVienId = nhanVienid;
                newItem.QuyetDinhID = quyetDinhID;
                db.NSQuaTrinhCongTacs.InsertOnSubmit(newItem);
                db.SubmitChanges();


                wdThayDoiCoQuanSuDung.Hide();

                ((NhanSu)this.Page).LoadGrid(hdNodeId.Text, "");
            }

        }
        public void ShowDialogThayDoiCoQuan(string nhanVienId, string nodeName, string nodeId)
        {
            wdThayDoiCoQuanSuDung.Show();
            hdNhanVienId.Text = nhanVienId;
            NSNhanVien nhanVienItem = nhanVien.GetById(Convert.ToInt32(nhanVienId));
            hdNodeId.Text = nodeId;
            lblCoQuanSuDungHienTai.Text = toChuc.LayTenToChuc(nhanVienItem.CoQuanQuanLiId.Value);
            txtCoQuanSuDung_ThayDoiCoQuan.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtCongViec.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            UCQuyetDinh.ResetControl(common.QuaTrinhCongTac);
        }

        [DirectMethod]
        public void LoadLookUpToChuc_QTCT()
        {
            UcToChuc1.ShowLookUp_ToChuc("", true, "");
        }
    }
}